package net.zjitc.controller;

import com.github.pagehelper.PageInfo;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import net.zjitc.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @AUTHOR zhj
 * @DATE 2019/4/10 16:07
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;
    @RequestMapping("findPoroductByCid")
    public String findPoroductByCid(@RequestParam(required = false) Integer cid, Model model){
        //页面需要的数据有 种类+数量+商品
        Category category = categoryService.findByCid(cid);

       List<Product> list =  productService.findByCid(cid);

       model.addAttribute("pList",list);
       model.addAttribute("category",category);
       model.addAttribute("pCount",list.size());
       return "/client/product_list.jsp";
    }

    //按分页查询
    @RequestMapping("showPagePoroductByCid")
    public String showPagePoroductByCid(@RequestParam(required = false) Integer cid,
                                               int currentPage,
                                               Model model){
        //页面需要的数据有 种类+数量(在pageInfo中)+商品
        PageBean<Product> pageBean =  productService.findPageProduct(cid,currentPage,6);
       model.addAttribute("p",pageBean);
       model.addAttribute("cid",cid);
       return "/client/product_pagelist.jsp";

    }
}
